/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency.Handlers;

import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.bukkit.entity.Player;

/**
 *
 * @author Sammy
 */
public class BankHandler {

    private static final File loans = new File("plugins/RPGmod/currency/loans.txt");

    public boolean HasLoan(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            if (prop.containsKey(Key)) {
                return true;
            }
        } catch (IOException ex) {
        }
        return false;
    }

    public void MakeLoan(Player p, int amount, int time, int ID, int IDRep) {
        Properties prop = new Properties();
        String Key = p.getName().toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            prop.put(Key, String.valueOf(amount + ";" + Math.round(amount * (1 + (CurrencyProperties.usury / 100))) + ";" + Math.round(amount * (1 + (CurrencyProperties.usury / 100))) + ";" + time + ";" + time * 24 + ";" + ID + ";" + IDRep));
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }

    public int GetloanQnt(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] qnt = values.split(";");
            return Integer.parseInt(qnt[0]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanToPay(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] qnt = values.split(";");
            return Integer.parseInt(qnt[1]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanRepaid(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] qnt = values.split(";");
            return Integer.parseInt(qnt[2]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanTime(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] Time = values.split(";");
            return Integer.parseInt(Time[3]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanTimeLeft(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] TimeLeft = values.split(";");
            return Integer.parseInt(TimeLeft[4]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanID(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] ID = values.split(";");
            return Integer.parseInt(ID[5]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public int GetloanIDRep(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] IDRep = values.split(";");
            return Integer.parseInt(IDRep[6]);
        } catch (Exception ex) {
        }
        return 0;
    }

    public Set<Entry<Object, Object>> GetLoans() {
        Properties prop = new Properties();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            return prop.entrySet();
        } catch (Exception ex) {
        }
        return null;
    }

    public void RepayLoan(Player p, int amount) {
        Properties prop = new Properties();
        String Key = p.getName().toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] Repay = values.split(";");
            int Initial = Integer.parseInt(Repay[2]) - amount;
            Repay[2] = String.valueOf(Initial);
            String propString = Repay[0] + ";" + Repay[1] + ";" + Repay[2] + ";" + Repay[3] + ";" + Repay[4] + ";" + Repay[5] + ";" + Repay[6];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }

    public void SetTimeLeft(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] TimeLeft = values.split(";");
            TimeLeft[4] = String.valueOf(amount);
            String propString = TimeLeft[0] + ";" + TimeLeft[1] + ";" + TimeLeft[2] + ";" + TimeLeft[3] + ";" + TimeLeft[4] + ";" + TimeLeft[5] + ";" + TimeLeft[6];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }

    public void SetSchedule(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] TimeLeft = values.split(";");
            TimeLeft[5] = String.valueOf(amount);
            String propString = TimeLeft[0] + ";" + TimeLeft[1] + ";" + TimeLeft[2] + ";" + TimeLeft[3] + ";" + TimeLeft[4] + ";" + TimeLeft[5] + ";" + TimeLeft[6];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }

    public void SetScheduleRep(String p, int amount) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            String values = prop.getProperty(Key);
            String[] TimeLeft = values.split(";");
            TimeLeft[6] = String.valueOf(amount);
            String propString = TimeLeft[0] + ";" + TimeLeft[1] + ";" + TimeLeft[2] + ";" + TimeLeft[3] + ";" + TimeLeft[4] + ";" + TimeLeft[5] + ";" + TimeLeft[6];
            prop.put(Key, propString);
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }

    public void RemoveLoan(String p) {
        Properties prop = new Properties();
        String Key = p.toLowerCase();
        try {
            FileInputStream in = new FileInputStream(loans);
            prop.load(in);
            prop.remove(Key);
            prop.store(new FileOutputStream(loans), "[Name]=amount;topay;repaid;time;TimeLeft;ID;IDRep");
        } catch (Exception ex) {
        }
    }
}
